﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：OrderManage.ascx
//
//文件功能描述：订单详细信息UI层
// 
//创建标识：2007.11.21 李鹏
//创建描述：
// 
//修改标识：2007.11.28李鹏
//修改描述：修改异常捕捉
//
// 
//修改标识：2007.11.29李鹏
//修改描述：修改订单项的分页\修改生成发货单操作
//
------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost;

public partial class UserControls_OrderDetail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            #region//查询订单详细信息

            //
            //获取要查询的订单号
            //
            string strOrderID = Convert.ToString(Request.QueryString["OrderInfoID"]);

            //
            //隐藏控件保存订单号
            hdfOrderID.Value = strOrderID;

            DocumentService documentService = new DocumentService();

            //
            //配置分页参数
            //
            int iPageSize = 10;
            int iPageIndex = 1;

            //
            //查询订单项，并绑定
            //
            DataSet ds = documentService.GetOrderItemForPaging(iPageSize, iPageIndex, strOrderID);
            if (ds != null)
            {
                GridviewBind(ds);
            }

            //
            //绑定订单的详细信息
            //
            GetOrderDetail();

            //
            //隐藏控件保存共有多少条记录
            //
            int iItems = documentService.GetOrderItemRows(strOrderID);
            hdfRecords.Value = iItems.ToString();

            //
            //绑定分页按钮
            //
            BindToPaging(iPageSize, iPageIndex);

            #endregion

        }//if (!this.IsPostBack)
    }


    /// <summary>
    /// 同步调用WEB服务,绑定订单的详细信息
    /// </summary>
    void GetOrderDetail()
    {
        #region//同步调用WEB服务,绑定订单的详细信息

        //
        //获取要查询的订单号
        //
        string strOrderID = hdfOrderID.Value.Trim();

        DocumentService documentService = new DocumentService();

        V_OrderInfo orderDetail = documentService.GetOrderDetail(strOrderID);
        this.lblOrderID.Text = orderDetail.OrderInfoID;
        //下单时间
        this.lblOrderDate.Text = orderDetail.CreationTime.ToShortDateString();
        #region//订单状态
        if (orderDetail.PaymentStatus)
        {
            this.lblOrderStatus.Text = "已付款";
            this.ibtnReceipt.Visible = false;
        }
        else
        {
            this.lblOrderStatus.Text = "未付款";
            this.ibtnReceipt.Visible = true;
        }
        if (orderDetail.ConsignmentStatus)
        {
            this.lblOrderStatus.Text += ",已发货";
            this.ibtnGenerateInvoice.Visible = false;
        }
        else
        {
            this.lblOrderStatus.Text += ",未发货";
            this.ibtnGenerateInvoice.Visible = true;
        }
        if (orderDetail.ArchivingStatus)
        {
            this.lblOrderStatus.Text += ",已归档";
            this.ibtnOrderPigeonhole.Visible = false;
            ibtnCancelPigeonhole.Visible = true;
        }
        else
        {
            this.lblOrderStatus.Text += ",未归档";
            this.ibtnOrderPigeonhole.Visible = true;
            ibtnCancelPigeonhole.Visible = false;
        }
        if (documentService.ExitInvoiceByOrderID(strOrderID))
        {
            //已生成发货单
            this.ibtnGenerateInvoice.Visible = false;
        }
        else
        {
            this.ibtnGenerateInvoice.Visible = true;
        }
        #endregion
        //配送方式
        this.lblSendStyle.Text = orderDetail.ShipMethod;
        //支付方式
        this.lblBillStyle.Text = orderDetail.BillMethod;
        //总金额
        this.lblTotal.Text = "￥" + orderDetail.TotalPrice.ToString();
        //购物者姓名
        this.lblPayerName.Text = orderDetail.BillToFirstName + orderDetail.BillToLastName;
        //购物者电话
        this.lblPayerPhone.Text = orderDetail.Phone;
        //收货人姓名
        this.lblInceptName.Text = orderDetail.ShipToFirstName + orderDetail.ShipToLastName;
        //收货人地址
        this.lblInceptAddr.Text = orderDetail.ShipAddress;
        //收货人所在城市
        this.lblCity.Text = orderDetail.ShipCity;
        //收货人所在省份
        this.lblProvince.Text = orderDetail.ShipProvince;
        //收货人邮编
        this.lblPostcode.Text = orderDetail.ShipZip;
        #endregion
    }


    /// <summary>
    /// 取消订单归档
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnCancelPigeonhole_Click(object sender, ImageClickEventArgs e)
    {
        //
        //配置取消订单归档所需的参数
        //
        string strOrderID = Convert.ToString(Request.QueryString["OrderInfoID"]);
        int iSaleManID = Convert.ToInt32(Session["userID"]);

        DocumentService documentService = new DocumentService();

        //
        //取消订单归档
        //
        if (documentService.UpdateArchivingStatus(strOrderID,iSaleManID,true))
        {
            ibtnOrderPigeonhole.Visible = true;
            ibtnCancelPigeonhole.Visible = false;
            GetOrderDetail();
        }
        else
        {
            ibtnOrderPigeonhole.Visible = false;
            ibtnCancelPigeonhole.Visible = true;
        }//if
    }


    /// <summary>
    /// 订单归档
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnOrderPigeonhole_Click(object sender, ImageClickEventArgs e)
    {
        //
        //配置订单归档所需的参数
        //
        string strOrderID = Convert.ToString(Request.QueryString["OrderInfoID"]);
        int iSaleManID = Convert.ToInt32(Session["userID"]);

        DocumentService documentService = new DocumentService();

        //
        //订单归档
        //
        if (documentService.UpdateArchivingStatus(strOrderID,iSaleManID,false))
        {
            ibtnOrderPigeonhole.Visible = false;
            ibtnCancelPigeonhole.Visible = true;
            GetOrderDetail();
        }
        else
        {
            ibtnOrderPigeonhole.Visible = true;
            ibtnCancelPigeonhole.Visible = false;
        }//if
    }


    /// <summary>
    /// 确认已收款
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnReceipt_Click(object sender, ImageClickEventArgs e)
    {
        //
        //配置确认已收款所需的参数
        //
        string strOrderID = Convert.ToString(Request.QueryString["OrderInfoID"]);
        int iSaleManID = Convert.ToInt32(Session["userID"]);

        DocumentService documentService = new DocumentService();

        //
        //确认已收款
        //
        if (documentService.CreateReceipt(strOrderID, iSaleManID))
        {
            this.ibtnReceipt.Visible = false;
            GetOrderDetail();
        }
        else
        {
            this.ibtnReceipt.Visible = true;
        }//if
    }


    /// <summary>
    /// 生成发货单
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnGenerateInvoice_Click(object sender, ImageClickEventArgs e)
    {
        //
        //配置生成发货单所需的参数
        //
        string strOrderID = Convert.ToString(Request.QueryString["OrderInfoID"]);
        int iSaleManID = Convert.ToInt32(Session["userID"]);

        DocumentService documentService = new DocumentService();

        //
        //生成发货单
        //
        if (documentService.CreateInvoice(strOrderID, iSaleManID))
        {
            this.ibtnGenerateInvoice.Visible = false;
            GetOrderDetail();
        }
        else
        {
            this.ibtnGenerateInvoice.Visible = true;
        }
    }

    /// <summary>
    /// 取消，返回订单管理页面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnCancel_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("OrderManage.aspx");  
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="ds"></param>
    private void GridviewBind(DataSet ds)
    {

        this.gdvOrder.DataSource = ds.Tables[0].DefaultView;
        this.gdvOrder.DataBind();
    }

    #region //绑定分页按钮
    void BindToPaging(int iPageSize, int iPageIndex)
    {
        DocumentService documentService = new DocumentService();
        //
        //读取订单数
        //
        int iOrders = Convert.ToInt32(hdfRecords.Value.Trim());

        if (iOrders > 0)
        {
            //
            //有订单
            //

            //
            //订单数
            //
            lblRecordCount.Text = "共 " + iOrders.ToString() + " 条";
            lblPageCount.Visible = true;
            lblNoRecord.Visible = false;

            //
            //计算总页数
            //
            int iPages = iOrders / iPageSize;
            if (iOrders % iPageSize != 0)
            {
                iPages += 1;
            }//if(iOrders % iPageSize != 0)
            lblPageCount.Text = "共 " + iPages.ToString() + " 页";
            lblPageCount.Visible = true;

            //
            //有多页
            //
            if (iPages > 1)
            {
                btnFirst.Visible = true;
                btnPrev.Visible = true;
                btnNext.Visible = true;
                btnLast.Visible = true;
            }
            else
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
            }//if (iPages > 1)

            //
            //当前页
            //
            lblCurrentIndex.Text = "第 " + iPageIndex + " 页";
            lblCurrentIndex.Visible = true;
        }
        else
        {
            //
            //没有订单
            //
            lblNoRecord.Visible = true;
            lblPageCount.Visible = false;
            lblRecordCount.Visible = false;
            lblCurrentIndex.Visible = false;

            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;

        }//if(iOrders > 0)
    }

    #endregion

    #region  //翻页
    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        //
        //获取当前页
        //
        string strPageIndex = lblCurrentIndex.Text.Trim();
        strPageIndex = strPageIndex.Substring(2);
        strPageIndex = strPageIndex.Substring(0, strPageIndex.Length - strPageIndex.LastIndexOf(" "));
        int iPageIndex = Convert.ToInt32(strPageIndex);

        //
        //获取总页数
        //
        string strPageCount = lblPageCount.Text.Trim();
        strPageCount = strPageCount.Substring(2);
        strPageCount = strPageCount.Substring(0, strPageCount.Length - strPageCount.LastIndexOf(" "));
        int iPageCount = Convert.ToInt32(strPageCount);

        //
        //判断操作类型
        //
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "prev"://上一页
                if (iPageIndex > 1)
                {
                    iPageIndex -= 1;
                }
                break;
            case "next"://下一页
                if (iPageIndex < iPageCount)
                {
                    iPageIndex += 1;
                }
                break;
            case "last"://最后一页
                iPageIndex = iPageCount;
                break;
            default://首页
                iPageIndex = 1;
                break;
        }//switch (arg)

        //
        //配置查询条件
        //
        int iPageSize = 10;
        string strOrderID = hdfOrderID.Value.Trim();

        //
        //绑定下一页的数据
        //
        DataSet ds = (new DocumentService()).GetOrderItemForPaging(iPageSize, iPageIndex, strOrderID);
        GridviewBind(ds);

        //
        //绑定分页按钮
        //
        BindToPaging(iPageSize, iPageIndex);
    }
    #endregion
}
